package org.sippr.service.impl;

import org.sippr.mapper.WordClsMapper;
import org.sippr.pojo.WordCls;
import org.sippr.service.WordClsService;
import org.sippr.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class WordClsImpl implements WordClsService {
    @Autowired
    private WordClsMapper wordClsMapper;

    @Override
    public WordCls findByName(String name) {
        return wordClsMapper.findByName(name);
    }


    @Override
    public void add(String name, Integer parentId) {
        WordCls wordCls = new WordCls();

        wordCls.setParentId(parentId);
        wordCls.setName(name);
        Map<String, Object> map = ThreadLocalUtil.get();
        String id = (String) map.get("id");
        wordCls.setCreateUser(id);
        wordCls.setUpdateUser(id);
        wordCls.setCreateTime(LocalDateTime.now());
        wordCls.setUpdateTime(LocalDateTime.now());


        wordClsMapper.add(wordCls);
    }

    @Override
    public void update(WordCls wordCls) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String id = (String) map.get("id");
        wordCls.setUpdateUser(id);
        wordCls.setUpdateTime(LocalDateTime.now());

        wordClsMapper.update(wordCls);
    }

    @Override
    public void delete(Integer id) {
        wordClsMapper.delete(id);
    }

    @Override
    public List<WordCls> list() {
        return wordClsMapper.list();
    }

    @Override
    public List<WordCls> getListByParentId(Integer parentId) {
        return wordClsMapper.getListByParentId(parentId);
    }
}
